package com.d3works.apps.supplychain.service;

import com.d3works.apps.sale.db.SaleOrder;
import com.d3works.apps.sale.db.SaleOrderLine;
import com.d3works.apps.sale.db.repo.SaleOrderLineRepository;
import com.d3works.apps.supplychain.exception.SupplychainExceptionMessage;
import com.d3works.exception.D3worksException;
import com.d3works.exception.db.repo.TraceBackRepository;
import com.d3works.i18n.I18n;
import java.util.ArrayList;
import java.util.List;

public class SaleOrderCheckAnalyticServiceImpl implements SaleOrderCheckAnalyticService {

  @Override
  public void checkSaleOrderLinesAnalyticDistribution(SaleOrder saleOrder) throws D3worksException {

    List<String> productList = new ArrayList<>();
    for (SaleOrderLine saleOrderLine : saleOrder.getSaleOrderLineList()) {
      if (saleOrderLine.getTypeSelect() == SaleOrderLineRepository.TYPE_NORMAL
          && saleOrderLine.getAnalyticDistributionTemplate() == null) {
        productList.add(saleOrderLine.getProductName());
      }
    }
    if (!productList.isEmpty()) {
      throw new D3worksException(
          TraceBackRepository.CATEGORY_MISSING_FIELD,
          I18n.get(SupplychainExceptionMessage.SALE_ORDER_ANALYTIC_DISTRIBUTION_ERROR),
          productList);
    }
  }
}
